#include "widget_funcb_smart_vision.h"
#include "ui_widget_funcb_smart_vision.h"

Widget_funcB_Smart_vision::Widget_funcB_Smart_vision(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget_funcB_Smart_vision)
{
    ui->setupUi(this);
    int maxWidth = 400;
    int maxHeight = 200;
    // 加载图像
    QPixmap pixmap(img_path);

      // 检查图像是否有效
      if (pixmap.isNull()) {
          qDebug() << "无法加载图像!";
          return;
      }

      // 获取图像的原始宽度和高度
      int width = pixmap.width();
      int height = pixmap.height();

      // 计算缩放比例，保持纵横比
      float aspectRatio = static_cast<float>(height) / width;

      // 根据最大宽度和最大高度计算新的尺寸
      int newWidth = width;
      int newHeight = height;

      if (width > maxWidth) {
          newWidth = maxWidth;
          newHeight = static_cast<int>(maxWidth * aspectRatio);
      }

      if (newHeight > maxHeight) {
          newHeight = maxHeight;
          newWidth = static_cast<int>(maxHeight / aspectRatio);
      }

      // 缩放图像
      pixmap = pixmap.scaled(newWidth, newHeight, Qt::KeepAspectRatio);

      // 设置缩放后的图像到 QLabel 上
      ui->image_label->setPixmap(pixmap);
}

Widget_funcB_Smart_vision::~Widget_funcB_Smart_vision()
{
    delete ui;
}

void Widget_funcB_Smart_vision::Show_image()
{

    int maxWidth = 400;
    int maxHeight = 200;
    // 加载图像
    QPixmap pixmap(img_path);

      // 检查图像是否有效
      if (pixmap.isNull()) {
          qDebug() << "无法加载图像!";
          return;
      }

      // 获取图像的原始宽度和高度
      int width = pixmap.width();
      int height = pixmap.height();

      // 计算缩放比例，保持纵横比
      float aspectRatio = static_cast<float>(height) / width;

      // 根据最大宽度和最大高度计算新的尺寸
      int newWidth = width;
      int newHeight = height;

      if (width > maxWidth) {
          newWidth = maxWidth;
          newHeight = static_cast<int>(maxWidth * aspectRatio);
      }

      if (newHeight > maxHeight) {
          newHeight = maxHeight;
          newWidth = static_cast<int>(maxHeight / aspectRatio);
      }

      // 缩放图像
      pixmap = pixmap.scaled(newWidth, newHeight, Qt::KeepAspectRatio);

      // 设置缩放后的图像到 QLabel 上
      ui->image_label->setPixmap(pixmap);
}

void Widget_funcB_Smart_vision::on_open_file_Button_clicked()
{
    // 打开文件
    img_path = QFileDialog::getOpenFileName(this,
                                            "选择一张图片",
                                            "D:/测试/",
                                            "Images (*.png *.xpm *.jpg)");
    if(img_path.isEmpty())
    {
        // 弹窗
        QMessageBox::warning(this,"警告" , "请选择一张图片");
    }

    QPixmap img = QPixmap(img_path);
    // 显示到标签上
    Show_image();

    // 测试
    image_Base64 = My_Image::Image_to_Base64(img_path);
    qDebug() << image_Base64;
}

void Widget_funcB_Smart_vision::on_Token_Button_clicked()
{
    // 运行 HTTP 的获取 AccessToken
    Http.get_AccessToken();
}


void Widget_funcB_Smart_vision::on_pushButton_clicked()
{
    // 开始识别
    Http.get_plate_number(image_Base64);

    ui->output_label->setText(Http.get_data());
}
